﻿using DataService.Cache.IService;
using DataService.Core.Container;
using Microsoft.Extensions.Caching.Memory;
using Mysqlx.Crud;
using System;
using System.Collections.Generic;
using System.Text;

namespace UnitTest
{
    public class TestIOC
    {
        public void TestScope()
        {
            Func<string, object> d = x => new List<string>();

            //DataService.Core.Container
            var ioc = new ObjectContainer();
            var services = ioc.Services;

            string ddd = "容器注入参数";
            ioc.AddScoped<P1>(p => new S1());
            ioc.AddScoped<P2>(d => new S2(ddd));
            ioc.AddScoped<P3>(p => new S3());

            ioc.AddTransient<P4>(p => new S4());
            ioc.AddTransient<P4>(p => new S5());
            ioc.AddTransient<P4>(p => new S6());
            IServiceProvider serviceProvider = ioc.BuildServiceProvider();

            var p1 = serviceProvider.GetService<P1>();
            p1.Test1();

            P2 p2 = serviceProvider.GetService<P2>();
            p2.Test2();
            Console.WriteLine($"serviceProvider.GetService<P2>：{p2}");

            P2 p2_1= ioc.Resolve<P2>();
            Console.WriteLine($"ioc.Resolve：{p2_1}" );

            Console.WriteLine("*************测试注册*************");
            ioc.Register(typeof(S2), null, null);
            var s2 = ioc.Resolve(typeof(S2));
            Console.WriteLine($" ioc.Resolve(typeof(S2))：{s2}");

            
            ioc.Register(typeof(P5), typeof(P5_S5_1), null);
            var mc = ioc.Resolve(typeof(P5));
           P5 p5= serviceProvider.GetService<P5>();
            p5.Test5();
            Console.WriteLine($"ioc.Resolve(typeof(P5))：{mc}");
            Console.WriteLine("*************测试注册*************");

            object objp3 = serviceProvider.GetService(typeof(P3));
            Console.WriteLine(objp3);

            P3 p3_1 = (P3)objp3;
            p3_1.Test3();
            List<P4> p4s = serviceProvider.GetServices<P4>().ToList();
            foreach (P4 p4 in p4s)
            {
                p4.Test4();
            }
        }



        public void TestAppcation()
        {
            //DataService.Core.Container

            string ddd = "容器注入参数";
            ObjectContainer.Current.AddScoped<P1>(p => new S1());
            ObjectContainer.Current.AddScoped<P2>(d => new S2(ddd));
            ObjectContainer.Current.AddScoped<P3>(p => new S3());

            ObjectContainer.Current.AddTransient<P4>(p => new S4());
            ObjectContainer.Current.AddTransient<P4>(p => new S5());
            ObjectContainer.Current.AddTransient<P4>(p => new S6());

            var p1 = ObjectContainer.Provider.GetService<P1>();
            p1.Test1();

            P2 p2 = ObjectContainer.Provider.GetService<P2>();
            p2.Test2();

            object objp3 = ObjectContainer.Provider.GetService(typeof(P3));
            Console.WriteLine(objp3);

            P3 p3_1 = (P3)objp3;
            p3_1.Test3();
            List<P4> p4s = ObjectContainer.Provider.GetServices<P4>().ToList();
            foreach (P4 p4 in p4s)
            {
                p4.Test4();
            }



            Console.WriteLine("*************测试注册*************");
            ObjectContainer.Current.Register(typeof(S2), null, null);
            var s2 = ObjectContainer.Current.Resolve(typeof(S2));
            Console.WriteLine($" ioc.Resolve(typeof(S2))：{s2}");


            ObjectContainer.Current.Register(typeof(P5), typeof(P5_S5_1), null);
            var mc = ObjectContainer.Current.Resolve(typeof(P5));

            P5 p5 = ObjectContainer.Provider.GetService<P5>();

            p5.Test5();
            Console.WriteLine($"ioc.Resolve(typeof(P5))：{mc}");
            Console.WriteLine("*************测试注册*************");


        }
    }

    #region 测试无参构造 - 依赖注入
    public interface P1
    {
        void Test1();
    }

    public class S1 : P1
    {

        public void Test1()
        {
            Console.WriteLine("P1->S1");
        }
    }
    #endregion

    #region 测试有参依赖注入
    public interface P2
    {

        string Test2();
    }

    public class S2 : P2
    {

        public S2()
        {
            Console.WriteLine("无参构造函数");
        }
        public S2(string s)
        {
            Console.WriteLine($"有参构造函数：{s}");
        }

        public string Test2()
        {
            Console.WriteLine("P2->S2");
            return "P2->S2";
        }
    }
    #endregion


    #region 测试 object 依赖注入
    public interface P3
    {
        void Test3();
    }

    public class S3 : P3
    {

        public void Test3()
        {
            Console.WriteLine("P3->S3");
        }
    }
    #endregion

    #region 获取一批 依赖注入
    public interface P4
    {
        void Test4();
    }

    public class S4 : P4
    {

        public void Test4()
        {
            Console.WriteLine("P4->S4-1");
        }
    }


    public class S5 : P4
    {

        public void Test4()
        {
            Console.WriteLine("P4->S4-2");
        }
    }
    public class S6 : P4
    {

        public void Test4()
        {
            Console.WriteLine("P4->S4-3");
        }
    }
    #endregion
    #region  测试注册
    public interface P5
    {
        void Test5();
    }

    public class P5_S5_1 : P5
    {

        public void Test5()
        {
            Console.WriteLine("P5->S5");
        }
    }
    #endregion


}
